package com.partners.controller.Page;

import com.alibaba.fastjson.JSONObject;
import com.partners.annotation.Auths;
import com.partners.constant.PermissionEnum;
import com.partners.service.ExchangeService;
import com.partners.service.UserService;
import com.partners.utils.CodeUtils;
import com.partners.utils.SessionManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @author liangyongle
 * @date 2018-12-21
 */
@Controller
@RequestMapping("pointsPage")
@Slf4j
public class PointsPageController {


    @Resource
    private SessionManager sessionManager;

    @Resource
    private UserService userService;

    @Resource
    private ExchangeService exchangeService;


    /**
     * 积分明细
     * @param
     * */
    @GetMapping("pointsList")
    @Auths
    public ModelAndView addCustomer(HttpSession session){
        JSONObject jsonObject = sessionManager.JsonSession(session);
        Integer uid = jsonObject.getInteger("userId");
        //查询是否是管理员才有的权限
        Integer code = PermissionEnum.ALL_POINTS.getCode();
        boolean permission = userService.hasPermission(uid, code);
        Integer all = 0;
//        Integer code1 = PermissionEnum.MY_POINTS.getCode();
//        boolean permission1 = userService.hasPermission(uid, code1);
        if(permission){all = 1;}
        ModelAndView mav =new ModelAndView("show/point_list");
        mav.addObject("uid",uid);
        mav.addObject("all",all);
        return mav;
    }


    /**
     * 积分推荐
     * @param
     * */
    @GetMapping("recommend")
    @Auths
    public ModelAndView pointsRecommend(HttpSession session){
        JSONObject jsonObject = sessionManager.JsonSession(session);
        Integer uid = jsonObject.getInteger("userId");
        //判断是否有权限进入
        Integer code = PermissionEnum.PRODUCT_RECOMMEND.getCode();
        boolean permission = userService.hasPermission(uid, code);
        String viewName = "show/points_recommend";
        ModelAndView mav = CodeUtils.checkPermissionView(permission, viewName, code, uid);
        return mav;
    }

    /**
     * 积分详情
     * @param
     * */
    @GetMapping("points_detail/{id}")
    public ModelAndView pointsDetail(@PathVariable("id") Integer id){
        ModelAndView mav =new ModelAndView("show/points_detail");
        mav.addObject("id",id);
        return mav;
    }

    /**
     * 用户的兑换列表
     * */

    @GetMapping("exchange_list")
    @Auths
    public ModelAndView getMyExchangeList(HttpSession session){
        JSONObject jsonObject = sessionManager.JsonSession(session);
        Integer uid = jsonObject.getInteger("userId");
        ModelAndView mav =new ModelAndView("show/myexchange_list");
        mav.addObject("uid",uid);
        return mav;
    }

    /**
     * 奖品核销
     * @param
     * */
    @GetMapping("verify")
    @Auths
    public ModelAndView sendVerifyExchange(HttpSession session,@RequestParam("id") Integer id){
        JSONObject jsonObject = sessionManager.JsonSession(session);
        Integer uid = jsonObject.getInteger("userId");
        if(id == null){
            ModelAndView mav = new ModelAndView();
            mav.setViewName("show/error");
            mav.addObject("code",404);
            mav.addObject("msg","无操作权限");
            return mav;
        }else{
            Integer code = PermissionEnum.PRODUCT_VERIFY.getCode();
            boolean permission = userService.hasPermission(uid, code);
            ModelAndView mav = new ModelAndView();
            if(permission){
                Map<String, Object> result = exchangeService.sendVerifyExchange(id, uid);
                if((Integer) result.get("code") == 200){
                    mav.setViewName("show/verify_exchange");
                    mav.addObject("uid",uid);
                }else{
                    mav.setViewName("show/error");
                    mav.addObject("code",result.get("code").toString());
                    mav.addObject("msg",result.get("msg"));
                }
            }else{
                mav.setViewName("show/error");
                mav.addObject("code",404);
                mav.addObject("msg","无操作权限");
            }
            return mav;
        }
    }




}
